import java.util.ArrayList;
import java.util.Iterator;

/**
 * Create all numbers lucky possibles and handle these numbers.
 * This class extend ArrayList class
 * @author Kao Vang
 *
 */
public class ListLuckyNumbers extends ArrayList<LuckyNumbers>{
	
	/**
	 * Default serial ID
	 */
	private static final long serialVersionUID = 1L;
	
	/* Attributes */
	private int numberTotalOfLuckyNumber;					// The number total of lucky numbers
	private Strategy strategies ;
	private int numbers_search_ind;
	
	/**
	 * Construcrtor
	 */
	public ListLuckyNumbers(Strategy stra) {
		super();
		this.strategies = stra;
		this.numberTotalOfLuckyNumber =0;
		numbers_search_ind = -1;
		CreatAllLuckyNumbers();
	}
	
	/**
	 * Create & add all numbers to list
	 */
	public void CreatAllLuckyNumbers() {
		int i,j,k,m,n;
		i=0;
		for(;i<=Constants.NUMBER_OF_DIGIT;i++){
			j=0;
			for(;j<=Constants.NUMBER_OF_DIGIT;j++){
				k=0;
				for(;k<=Constants.NUMBER_OF_DIGIT;k++){
					this.add(new LuckyNumbers(i,j,k)); // Create & add a luckNumber to list 
					this.get(this.numberTotalOfLuckyNumber).setNumber_draw(this.numberTotalOfLuckyNumber);//Set the number for each Lucky numbers 
					this.numberTotalOfLuckyNumber++;
				}//end k				
			}//end j
		}// end i

		System.out.println(this.numberTotalOfLuckyNumber + " lucky numbers are added to list -> Ready");
	}
	
	
	/**
	 * Desactivated all luckyNumbers
	 */
	public void initial() {
		Iterator<LuckyNumbers> iterator = this.iterator();
		while (iterator.hasNext()) {
			//System.out.println(iterator.next());
			iterator.next().setStatus(Constants.ACTIVE_NUMBER);
		}
		this.numbers_search_ind = -1;
	}
	
	/**
	 * Display the specifice luckyNumber
	 * @param index - A specific index to display
	 */
	public void displayLuckyNumber(int index) {
		System.out.println(this.get(index));
	}


	/**
	 * Show all lucky number present in listLuckyNumbers and its status is True
	 */
	public void showAllLuckyNum() {
		int line, cmt;
		Iterator<LuckyNumbers> iterator = this.iterator();
		line = 0;
		cmt =0;
		while (iterator.hasNext()) {
			System.out.print(iterator.next() +"   ");
			line ++;
			cmt++;
			if (line>=5){
				System.out.print("\n");
				line=0;
			}
		}
		System.out.println("Lucky numbers total: "+ cmt);
	}
	
	
	/**
	 * Show all lucky number present in listLuckyNumbers and its status is True
	 */
	public void showAllLuckyResultNum() {
		int line, cmt, i;
		LuckyNumbers lNumCur;
		Iterator<LuckyNumbers> iterator = this.iterator();
		line = 0;
		cmt =0;
		i=0;
		while (iterator.hasNext()) {
			lNumCur = iterator.next();
			if(lNumCur.getStatus()){
				if(i == this.numbers_search_ind){
					System.out.print("{"+lNumCur+"}   ");
				}else{
					System.out.print(lNumCur+"   ");
				}
				line ++;
				cmt++;
				if (line>= Constants.NUMBER_OF_L_NUMBER_PRINT_IN_LINE){
					System.out.print("\n");
					line=0;
				}
			}
			i++;
		}
		System.out.println("\nLucky numbers result total: "+ cmt);
	}
	
	
	/**
	 * Update all lucky numbers according to the stretagies determin
	 */
	public void updateLuckyNumbers() {
		LuckyNumbers luckyNumCur;
		Iterator<LuckyNumbers> iterator = this.iterator();
		while (iterator.hasNext()) {
			luckyNumCur = iterator.next();
			// Update lucky numbers according to number of Odd/Even strategy
			if(this.strategies.getNumOdd() != -1){
				if(!luckyNumCur.isOddEgual(this.strategies.getNumOdd())){
					luckyNumCur.setStatus(false);
				}
			}
			
			//Update lucky numbers according to Eliminated number strategy 
			if(!strategies.isEliminatedNumEmpty() && luckyNumCur.getStatus() ){
				if(strategies.isContainsANum(luckyNumCur)){
					luckyNumCur.setStatus(false);
				}
			}
			
			//TODO update strategy Double digit
			if(!this.strategies.isDouble() && luckyNumCur.getStatus() ){
				if(luckyNumCur.checkIsDouble()){
					luckyNumCur.setStatus(false);
				}
			}
			
			//if(luckyNumCur.isExistANum(this.strategies.getNumbersEliminates())){
			//	luckyNumCur.setStatus(false);
			//}
			
			
		}
	}
	
	

	/**
	 * Show a specific lucky number in list
	 * @param index - The index of the lucky Number
	 */
	public void showLuckyNumberAtIndex(int index) {
		this.get(index).showLuckyNum();
	}
	
	/**
	 * Show a lucky number that a number in parameter is present in there
	 * @param myInt - A number to test
	 */
	public void showLuckyIfExist(int myInt){
		LuckyNumbers luckyNumCur;
		Iterator<LuckyNumbers> iterator = this.iterator();
		while (iterator.hasNext()) {
			luckyNumCur = iterator.next();
			if(luckyNumCur.isExistADigit(myInt)){
				luckyNumCur.showLuckyNum();
			}
		}
	}
	
	/**
	 * Get the numbers of lucky numbers result
	 * @return - The number result
	 */
	public int getNumberOfResult() {
		int cnt = 0;
		Iterator<LuckyNumbers> iterator = this.iterator();
		while (iterator.hasNext()) {
			if(iterator.next().getStatus()){
				cnt++;
			}
		}
		return cnt;
	}
	
	/**
	 * Print the lucky numbers informations
	 */
	public void printLuckyNumbersInfo() {
		int cnt = 0;
		Iterator<LuckyNumbers> iterator = this.iterator();
		while (iterator.hasNext()) {
			cnt++;
			System.out.println("\n" + cnt);
			iterator.next().showInfo();
		}
	}
	
	/**
	 * Find a lucky if it exist in this list
	 * @param aLNum - The lucky number to test
	 * @return - Return the lucky number fond
	 */
	public LuckyNumbers findAluckyNumber(LuckyNumbers aLNum){
		for(int i=0;i<this.size();i++){
			if(this.get(i).isEguals(aLNum)){
				return this.get(i);
			}
		}
		return null;
	}
	
	/**
	 * Find a lucky if it exist in this list and it is a active number
	 * @param aLNum - The lucky number to test
	 * @return - Return the lucky number fond
	 */
	public LuckyNumbers findAluckyNumberActived(LuckyNumbers aLNum){
		for(int i=0;i<this.size();i++){
			if(this.get(i).isEguals(aLNum) && this.get(i).getStatus()){
				return this.get(i);
			}
		}
		return null;
	}

	public int getNumbers_search_ind() {
		return numbers_search_ind;
	}

	public void setNumbers_search_ind(int numbers_search_ind) {
		this.numbers_search_ind = numbers_search_ind;
	}
	
	
}
